package com.example.gpstest;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class DeviceGpsListener implements LocationListener {
	static final String tag = "DeviceListener"; // for Log
	StringBuilder sb;
	int noOfFixes = 0;
	TextView txtInfo;
	Activity activity=null;
	
	public DeviceGpsListener(Activity activity){
		this.activity=activity;
		txtInfo = (TextView) activity.findViewById(R.id.textDevice);
	}
	
	public void onLocationChanged(Location location) {
		Log.v(tag, "Location Changed (Device)");

		sb = new StringBuilder(512);
		
		noOfFixes++;

		/* display some of the data in the TextView */

		sb.append("No. of Fixes: ");
		sb.append(noOfFixes);
		sb.append('\n');
		sb.append('\n');

		sb.append("Londitude: ");
		sb.append(location.getLongitude());
		sb.append('\n');

		sb.append("Latitude: ");
		sb.append(location.getLatitude());
		sb.append('\n');

		sb.append("Altitiude: ");
		sb.append(location.getAltitude());
		sb.append('\n');

		sb.append("Accuracy: ");
		sb.append(location.getAccuracy());
		sb.append('\n');

		sb.append("Timestamp: ");
		sb.append(location.getTime());
		sb.append('\n');

		txtInfo.setText(sb.toString());
	}

	public void onProviderDisabled(String provider) {
		Log.v(tag, "Disabled (Device)");

		/* bring up the GPS settings */
		Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		activity.startActivity(intent);
	}

	public void onProviderEnabled(String provider) {
		Log.v(tag, "Enabled (Device)");
		Toast.makeText(activity, "GPS Enabled (Device)", Toast.LENGTH_SHORT).show();

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		/* This is called when the GPS status alters */
		switch (status) {
		case LocationProvider.OUT_OF_SERVICE:
			Log.v(tag, "Status Changed: Out of Service (Device)");
			Toast.makeText(activity, "Status Changed: Out of Service (Device)", Toast.LENGTH_SHORT).show();
			break;
		case LocationProvider.TEMPORARILY_UNAVAILABLE:
			Log.v(tag, "Status Changed: Temporarily Unavailable (Device)");
			Toast.makeText(activity, "Status Changed: Temporarily Unavailable (Device)", Toast.LENGTH_SHORT).show();
			break;
		case LocationProvider.AVAILABLE:
			Log.v(tag, "Status Changed: Available (Device)");
			Toast.makeText(activity, "Status Changed: Available (Device)",Toast.LENGTH_SHORT).show();
			break;
		}
	}
}
